package com.gdface.face.greendao_test;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import net.gdface.face.DaoMaster;
import net.gdface.face.DaoSession;


/**
 * Created by hewenwen on 2017/11/28.
 */

public class MyApplication extends Application {

    private static   MyApplication application;
    private static Context mThis;
    private static String DB_name = "face.db";
    private static DaoSession daoSession;

    @Override
    public void onCreate() {
        super.onCreate();

        mThis = this;
        setupDatabase();
    }


    /**
     * 配置数据库
     */
    private static void setupDatabase() {
        //创建数据库
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(mThis, DB_name, null);
        //获取可写数据库
        SQLiteDatabase db = helper.getWritableDatabase();
        //获取数据库对象
        DaoMaster daoMaster = new DaoMaster(db);
        //获取Dao对象管理者
        daoSession = daoMaster.newSession();
    }

    public static DaoSession getDaoSession(){
        if(null == daoSession){
            setupDatabase();
        }
        return daoSession;
    }


}
